﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using DG.Tweening;


public class EnemyUI : MonoBehaviour,IEventListener {
    private CreatureData EnemyData;
    private Text Defence;
    private Text HP;
    private Text Attack;
    private Text HpValue;
    bool is_first = true;
    private Vector3 start;
    private Vector3 endValue;
    private void Awake()
    {
        HP = transform.Find("EnemyHP").GetComponent<Text>();
        Attack = transform.Find("EnemyAttack").GetComponent<Text>();
        Defence = transform.Find("EnemyDefence").GetComponent<Text>();
        HpValue = transform.Find("EnemyHP/Value").GetComponent<Text>();
        start = this.transform.position;
    }
    private void Start()
    {
        EventNodeCore.GetInstance().AttachEventListener(EventDef.PlayerAtk,this);
        EventNodeCore.GetInstance().AttachEventListener(EventDef.HpChange,this);
        endValue = new Vector3(start.x + 700f, start.y - 300, start.z);
        
    }
    private void OnEnable()
    {
        is_first = true;
    }
    private void OnDestroy()
    {
        EventNodeCore.GetInstance().DetachEventListener(-EventDef.Battle, this);
        EventNodeCore.GetInstance().DetachEventListener(EventDef.HpChange, this);
    }
    public int EventPriority()
    {
        return 0;
    }
    public bool HandleEvent(int id, object param1, object param2)
    {
        if (id == EventDef.PlayerAtk)
        {
            Debug.LogWarning("111");
            if (is_first)
            {
                //transform.DOMove(endValue, 1.0f);
                GameObject Enemy = (GameObject)param1;
                EnemyData = Enemy.GetComponent<CreatureData>();
                HP.text = "HP:";
                HpValue.text = EnemyData.HP.ToString();
                Attack.text = "Attack:" + EnemyData.atk.ToString();
                Defence.text = "Defence:" + EnemyData.def.ToString();
            }
            is_first = false;
        }
        if (id == EventDef.HpChange)
        {
            GameObject Enemy = (GameObject)param2;
            if (Enemy.tag == "Enemy")
            {
                int HpChange = (int)param1;
                EnemyData.HP += (int)HpChange;
                HpValue.text = EnemyData.HP.ToString();
            }
        }
        return false;
    }
}
